package pro.lingwu.station.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import pro.lingwu.station.exception.HasValueException;
import pro.lingwu.station.dto.db.Ticket;
import pro.lingwu.station.service.TicketService;

/**
 * @author @lingwu
 * @date created in 2021/11/16
 */
@Api(tags = "ticket")
@RestController
public class TicketController {

    private final TicketService service;

    public TicketController(TicketService service) {
        this.service = service;
    }

    @ApiOperation(value = "用户购票")
    @PostMapping("/ticket")
    public ResponseEntity<?> buyTicket(Ticket ticket) throws HasValueException {
        service.buyTicket(ticket);
        return ResponseEntity.ok("");
    }

    @ApiOperation(value = "用户查票")
    @GetMapping("/ticket")
    public ResponseEntity<?> listTickets(Ticket ticket){
        return ResponseEntity.ok(service.findTickets(ticket));
    }

    @ApiOperation(value = "改签")
    @PutMapping("/ticket/{id}")
    public ResponseEntity<?> updateTicket(Ticket ticket,
                                          @PathVariable int id){
        return ResponseEntity.ok("");
    }

    @ApiOperation(value = "退票")
    @DeleteMapping("/ticket/{id}")
    public ResponseEntity<?> giveUpTicket(String account,
                                          @PathVariable long id){
        Ticket ticket = new Ticket();
        ticket.setId(id);
        ticket.setAccount(account);
        service.rmTicket(ticket);
        return ResponseEntity.ok("");
    }

}
